package tr.gov.tubitak.bilgem.esya.certselector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tr.gov.tubitak.bilgem.esya.certviewer.viwerasn.ECertViewerCertificate;
import tr.gov.tubitak.uekae.esya.api.common.util.bag.Pair;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ramazan.girgin
 * Date: 10.10.2011
 * Time: 10:20
 * To change this template use File | Settings | File Templates.
 */
public class ReaderCertListWorker extends SwingWorker<List<List<byte[]>>,Pair<ReaderInfo,DefaultMutableTreeNode>>
{
    private static Logger logger = LoggerFactory.getLogger(ReaderCertListWorker.class);
    DefaultMutableTreeNode mCurrrentReaderNode;
    List<DefaultMutableTreeNode> mReaderNodeList;
    List<ReaderInfo> mReaderInfoList;
    List<DefaultMutableTreeNode> certNodeList=new ArrayList<DefaultMutableTreeNode>();
    JTree mTree;

    ReaderCertListWorker(JTree iTree,List<ReaderInfo> iReaderInfoList,List<DefaultMutableTreeNode> iReaderNodeList)
    {
        mReaderNodeList = iReaderNodeList;
        mReaderInfoList = iReaderInfoList;
        mTree = iTree;
    }

    public boolean ismShowOnlyQualified() {
        return mShowOnlyQualified;
    }

    public void setmShowOnlyQualified(boolean iShowOnlyQualified) {
        this.mShowOnlyQualified = iShowOnlyQualified;
    }

    boolean mShowOnlyQualified;

    @Override
    protected List<List<byte[]>> doInBackground() throws Exception
    {
        List<List<byte[]>> retList = new ArrayList<List<byte[]>>();
        //busyComponent.setBusy(true);

        for(int k=0;k<mReaderInfoList.size();k++)
        {
            try
            {
                ReaderInfo readerInfo = mReaderInfoList.get(k);
                EBasicSmartCard basicSmartCard = readerInfo.getBasicSmartCard();
                logger.debug(readerInfo.getmReaderName()+" icin session acilacak");
                basicSmartCard.openSession();
                logger.debug(readerInfo.getmReaderName()+" icin session acildi");
                logger.debug(readerInfo.getmReaderName()+" sertifikalar okunacak");
                ArrayList<ECertViewerCertificate> signatureCerts = basicSmartCard.getSignatureCerts();
                logger.debug(readerInfo.getmReaderName()+" sertifikalar okundu.");
                for (ECertViewerCertificate signatureCert : signatureCerts) {
                    if(mShowOnlyQualified && (!signatureCert.isQualifiedCertficate()))
                    {
                        continue;
                    }
                    logger.debug("sertifika bilgileri eklenecek");
                    CertInfo certInfo = new CertInfo(signatureCert);
                    readerInfo.addCertInfo(certInfo);
                    logger.debug(signatureCert.getSubject().toTitle()+"sertifika bilgileri eklendi");
                }
                List<ReaderInfo> publishList =new ArrayList<ReaderInfo>();
                publishList.add(readerInfo);
                publish(new Pair(readerInfo,mReaderNodeList.get(k)));
            }
            catch (Exception e)
            {
                throw e;
            }
            finally {
            }
        }

        return retList;
    }

    @Override
    protected void process(List<Pair<ReaderInfo,DefaultMutableTreeNode>> iReaderInfoList)
    {
        for(int k=0;k<iReaderInfoList.size();k++)
        {
            ReaderInfo readerInfo = iReaderInfoList.get(k).getObject1();
            DefaultMutableTreeNode node = iReaderInfoList.get(k).getObject2();
            List<CertInfo> certInfoList = readerInfo.getmCertInfoList();
            for(int m=0;m<certInfoList.size();m++)
            {
                logger.debug("Sertifika bilgisi agaca eklenecek");
                CertInfo certInfo = certInfoList.get(m);
                DefaultMutableTreeNode certNode = new DefaultMutableTreeNode(certInfo);
                certNodeList.add(certNode);
                node.add(certNode);
                logger.debug("Sertifika bilgisi agaca eklendi");
            }
            readerInfo.setLoaded(true);
            TreeUtil.expandTree(mTree, true);
        }

        if(isDone()){
            if(certNodeList.size() == 1)
            mTree.setSelectionPath(new TreePath(certNodeList.get(0).getPath()));
        }
    }
}
